package light.head.controller.site;

import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Msg;
import light.head.constant.Parm;
import light.head.constant.Url;
import light.head.dao.site.ParkOrganDAO;
import light.head.dao.sys.OrganDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;
import light.head.model.ParkOrgan;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 *	园区企业
 */
@Controller
@RequestMapping(Url.PARKORGAN)
public class ParkOrganController extends SupporterController{
	private ParkOrganDAO poDAO;
	private OrganDAO oDAO;
	
	@RequestMapping(value = Url.INIT)
	public ModelAndView init(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PARKORGAN_INIT);
	}

	@RequestMapping(Url.QUERY)
	public ModelAndView query(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.SORT, Parm.DELETED);
		if (poDAO.findByMap(map, pb) == RESULT.SUCCESS){
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}		
		return new ModelAndView(Url.PARKORGAN_LIST);
	}
	
	@RequestMapping(Url.EDIT)
	public ModelAndView edit(Integer code, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (null != code) {
			ParkOrgan po = poDAO.get(code);
			request.setAttribute(Parm.PARKORGAN, po);
		}
		return new ModelAndView(Url.PARKORGAN_DEIT);
	}
	@RequestMapping(Url.BATCHEDIT)
	public ModelAndView batchEdit( HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.PARKORGANBATCH_EDIT);
	}
	@RequestMapping(value = Url.SAVE)
	public void save(ParkOrgan po,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(po.getCode() != null){
			ParkOrgan p =poDAO.get(po.getCode());
			if(p.getSort() != po.getSort()){
				List<ParkOrgan> sList = poDAO.findSort(po.getSort());
				if(sList.size() != 0 && sList != null){
					print(response, Msg.PARAMETER_ERROR);
					return;
				}
			}
			result=poDAO.upd(po);
		}else{
			List<ParkOrgan> sList = poDAO.findSort(po.getSort());
			if(sList.size() != 0 && sList != null){
				print(response, Msg.PARAMETER_ERROR);
				return;
			}
			Date date=new Date();
			po.setDeleted(false);
			po.setCt(date);
			po.setCu(cu_code(request));
			result=poDAO.add(po);
		}
		if(result==RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	//批量添加
	@RequestMapping(value = Url.SAVEBATCH)
	public void saveBatch(ParkOrgan po,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if (po!=null) {
			for (int i = 0; i<po.getList().size();i++) {
				List<ParkOrgan> sList = poDAO.findSort(po.getList().get(i).getSort());
				if(sList.size() != 0 && sList != null){
					print(response, Msg.PARAMETER_ERROR);
					return;
				}
				if (po.getList().get(i).getName()!=null) {
					po.getList().get(i).setDeleted(false);
					po.getList().get(i).setCu(cu_code(request));
					po.getList().get(i).setCt(new Date());
					result = poDAO.add(po.getList().get(i));
				}
			}
		}
		if (result == RESULT.SUCCESS) {
			print(response, Msg.SUCCESS);
		} else {
			print(response, Msg.ERROR);
		}
	}
	@RequestMapping(value = Url.DELETE)
	public void delete(Integer code,Byte type,HttpServletRequest request, HttpServletResponse response) throws Exception {
		RESULT result = RESULT.ERROR;
		if(code!=null){
			ParkOrgan po = poDAO.get(code);
			po.setDeleted(true);
			result=poDAO.upd(po);
		}
		if(result==RESULT.SUCCESS){
			print(response, Msg.SUCCESS);
		}else{
			print(response, Msg.ERROR);
		}
	}
	
	/**
	 * 辅助查询公司
	 */
	@RequestMapping(Url.ADIPARKORGANINIT)
	public ModelAndView adiOrganInit(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView(Url.ADIPARKORGAN_INIT);
	}
	@RequestMapping(value = Url.ADIPARKORGANLIST)
	public ModelAndView adiOrganList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME,Parm.DELETED);
		List<Integer> ocs = poDAO.findOcs();
		if (oDAO.findByOc(ocs,map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.ADIPARKORGAN_LIST);
	}
	//批量辅助查询
	@RequestMapping(Url.PARKORGANBATCHORGANINIT)
	public ModelAndView batchOrganInit(Integer index, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setAttribute("index", index);
		return new ModelAndView(Url.PARKORGANBATCHORGAN_INIT);
	}
	@RequestMapping(value = Url.PARKORGANBATCHORGANLIST)
	public ModelAndView batchOrganList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		PageBean pb = createPageBean(request);
		Map<String, String> map = createQueryMapByParmName(request, Parm.NAME, Parm.DELETED);
		
		List<Integer> ocs = poDAO.findOcs();
		//ocs.add(Sys.ONE);
		if (oDAO.findByOc(ocs,map, pb) == RESULT.SUCCESS) {
			request.setAttribute(Parm.PAGE_BEAN, pb);
		}
		return new ModelAndView(Url.PARKORGANBATCHORGAN_LIST);
	}
			
	
	@Autowired
	public void setPoDAO(ParkOrganDAO poDAO) {
		this.poDAO = poDAO;
	}
	@Autowired
	public void setoDAO(OrganDAO oDAO) {
		this.oDAO = oDAO;
	}
}
